package com.ruoyi.aitapp.utils;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.aitapp.vo.UserRunTimeVO;
import com.ruoyi.aitcommon.config.ConfigManager;
import com.ruoyi.aitcommon.utils.LicenseUtils;
import com.ruoyi.aitcommon.utils.LogUtils;
import com.ruoyi.aitcommon.utils.MapUtils;
import com.ruoyi.common.core.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Component
public class APPRuntimeManager {
    @Autowired
    ConfigManager configManager;
    @Autowired
    APPDataUtils appDataUtils;
    @Autowired
    RedisCache redisCache;
    @Autowired
    LicenseUtils licenseUtils;
    private String prefix="APPRunTime:";
    private String UserInfoKey=prefix+"UserInfo";
    private String ApproveCntKey=prefix+"ApproveCnt";

    @PostConstruct
    public void onInit() {
        // 在这里编写你想在组件启动时执行的代码
    }

    /**
     * 设置用户审批信息
     * @param sqlParam
     * @throws Exception
     */
    public void setUserApproveInfo(String sqlParam) throws Exception {
        final Object sql = configManager.getContextByParm(sqlParam);
        final List<Map<String, Object>> mapList = appDataUtils.queryERP((String) sql);
        final Map<String, Object> userMap = MapUtils.newMap(mapList, "doUserCode");
        redisCache.setCacheMap(ApproveCntKey, userMap);
    }

    public List<Map<String, Object>> getUserApproveInfo() throws Exception {
        List<Map<String, Object>> retList = new ArrayList<>();
        final Map<String, Object> approveMap = redisCache.getCacheMap(ApproveCntKey);
        for(String key : approveMap.keySet()){
            retList.add((Map<String, Object>) approveMap.get(key));
        }
        return retList;
    }

    public int getUserApproveCnt(String userCode) {
        final Map<String, Object> approveMap = redisCache.getCacheMap(ApproveCntKey);
        int cnt = 0;
        if(approveMap.containsKey(userCode)){
            Map<String,Object> map= (Map<String, Object>)approveMap.get(userCode);
            cnt=(int)map.get("approveCnt");
        }
        return cnt;
    }

    /**
     * 新增用户运行信息
     * @param userCode 用户编码
     */
    public UserRunTimeVO addUserInfo(String userCode) {
        UserRunTimeVO userRunTimeVO=new UserRunTimeVO();
        userRunTimeVO.setUserCode(userCode);
        userRunTimeVO.setLoginTime(DateUtil.date().toString());
        userRunTimeVO.setStatus(0); //状态=登录
        updateUserInfo(userRunTimeVO);//更新redis
        LogUtils.info("登录信息:登录成功,用户[{}]",userCode);
        return userRunTimeVO;
    }

    /**
     * 更新用户信息至redis
     * @param userVO
     */
    public void updateUserInfo(UserRunTimeVO userVO){
        Map<String, UserRunTimeVO> userMap = redisCache.getCacheMap(UserInfoKey);
        if(userMap==null)
            userMap=new LinkedHashMap<>();
        userMap.put(userVO.getUserCode(),userVO);
        redisCache.setCacheMap(UserInfoKey,userMap);
    }
    public UserRunTimeVO getUserInfo(String userCode) {
        Map<String, UserRunTimeVO> userMap = redisCache.getCacheMap(UserInfoKey);
        if(userMap==null) return null;
        UserRunTimeVO userVO=null;
        if(userMap.containsKey(userCode)){
            userVO=userMap.get(userCode);
        }
        return userVO;
    }

    /**
     * 初始化用户信息
     * 如果用户存在,获取用户信息
     * 如果用户不存在,添加用户信息
     * @param userCode
     * @return
     */
    public UserRunTimeVO initUserInfo(String userCode) throws Exception {
        //校验 授权
        licenseUtils.validateLicense();
        //初始化登录信息
        UserRunTimeVO userInfo = this.getUserInfo(userCode);
        if(userInfo==null){
            //新增
            userInfo = this.addUserInfo(userCode);
        }else{
//            修改
            if(userInfo.getStatus()==1){
                //已经登出,重新修改登录状态
                userInfo = this.addUserInfo(userCode);
            }else{
                //未登出
            }
        }
        return userInfo;
    }

    public List<UserRunTimeVO> getUserInfoList(){
        Map<String, UserRunTimeVO> userMap = redisCache.getCacheMap(UserInfoKey);
        List<UserRunTimeVO> userList = new ArrayList<>();
        if(userMap==null)
            return userList;
        else{
            for (String key : userMap.keySet()){
                userList.add(userMap.get(key));
            }
        }
        return userList;
    }

    public int getActiveUserCnt(){
        final List<UserRunTimeVO> userInfoList = getUserInfoList();
        int cnt = 0;
        for(UserRunTimeVO userVO : userInfoList){
            if(userVO.getStatus()==0)
                cnt++;
        }
        return cnt;
    }
}
